<?php

/*
 * 发出http请求
 * $url 要请求的url地址
 */

function sendRequest($url) {
    if (function_exists('file_get_contents')) {
        $file_contents = file_get_contents($url);
    } else {
        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1); //启用POST提交
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $file_contents = curl_exec($ch);
        curl_close($ch);
    }
    return $file_contents;
}

/*
 * 返回数据解析
 * $str 字符串
 * $format 字符串类型
 */

function translationResult($str, $format = 'json') {
    if ($format == 'json') {
        $data = json_decode($str, true);
        if ($data === null) {
            if (strpos('订单已转过', $str) !== false) {
                $tid = isset($_REQUEST['order_id']) ? $_REQUEST['order_id'] : 0;
                if (defined('CURAPP') && CURAPP == 'jd') {
                    $str = $str . '<a href="' . JD_BASE_URL . '/paidang/orderCanel?order_id=' . $tid . '">点击取消</a>';
                } else {
                    $str = $str . '<a href="' . BASE_URL . '/paidang/orderCanel?order_id=' . $tid . '">点击取消</a>';
                }
                show_error($str);
            }
            show_error($str);
        }
        return $data;
    } elseif ($format == 'xml') {
        $pos = strpos($str, 'xml');
        if ($pos) {
            $xmlCode = simplexml_load_string($str, 'SimpleXMLElement', LIBXML_NOCDATA);
            return get_object_vars_final($xmlCode);
        } else {
            return '';
        }
    }
}

/*
 * xml数据节点获取
 * $obj xml对象
 */

function get_object_vars_final($obj) {
    if (empty($obj))
        $obj = "";
    elseif (is_object($obj)) {
        $obj = get_object_vars($obj);
    }
    if (is_array($obj)) {
        foreach ($obj as $key => $value) {
            $obj[$key] = get_object_vars_final($value);
        }
    }
    return $obj;
}

/*
 * 花集信誉计算公式
 * $point 总的信用分数
 */

function cal_grade($point) {
    if ($point <= 0) {
        return 1;
    }
    $point_arr = array(20, 50, 100, 180, 300,
        400, 600, 900, 1500, 2200,
        3000, 4000, 5500, 7000, 9000,
        13000, 18000, 25000, 50000, 100000,
        250000, 500000, 1000000, 2500000);
    $max = sizeof($point_arr) + 1;
    $grade = 0;
    foreach ($point_arr as $k => $v) {
        if ($point < $v) {
            $grade = $k + 1;
            break;
        }
    }
    if ($grade == 0) {
        $grade = $max;
    }
    return $grade;
}

/*
 * 相当于like功能,目前获取地区编号
 * $needle 要搜索的字符
 * $haystack 从该数组中搜索
 */

function array_preg_search($needle, $haystack) {
    if (!is_scalar($needle)) {
        return 0;
    }
    $needle = '/^' . $needle . '/';
    if (!is_array($haystack)) {
        return 0;
    }
    foreach ($haystack as $key => $val) {
        if (preg_match($needle, $val)) {
            return $key;
        }
    }
    return 0;
}

/*
 * 因为不同版本json_decode会把内容直接编码掉
 * $needle 要搜索的字符
 * $haystack 从该数组中搜索
 */

function my_json_decode($result, $assoc = false) {
    //5.4以上版本支持JSON_BIGINT_AS_STRING,
    if (version_compare(PHP_VERSION, '5.4.0') >= 0)
        $result = json_decode($result, $assoc, 512, JSON_BIGINT_AS_STRING);
    else
        $result = json_decode($result, $assoc);
    return $result;
}


function pageshow($per_page=0,$total_rows=0,$base_url='',$pwhere='')
{
    $CI =& get_instance();
    $pagecount = ceil($total_rows/$per_page);                        
    $CI->load->library('pagination');
    $config['page_query_string'] = TRUE;
    $config['base_url'] = $base_url.'/?c=index'.$pwhere;
	$config['total_rows'] = $total_rows;
	$config['per_page'] = $per_page; 		
	$config['uri_segment']=5;
    $val= '<li> <span>共 '.$pagecount.' 页 &nbsp;</span> </li>
	       <li> <span>到第
	       <input id="J_PageNum" type="text" name="J_PageNum" size="2" maxlengh="4" onkeyup="this.value=this.value.replace(/\D/g,1)">
	       <span>页</span>
           <input id="J_JumpTo" type="button" value="确定" class="basicBtn">
	       </span> </li>';
    
    $val .='<script type="text/javascript">
            $("#J_JumpTo").click(function(){
        	     var page = $("#J_PageNum").attr("value");
        		 window.location.href = "'.$base_url.'/?c=index&per_page="+page+"'.$pwhere.'";	
        	});
            </script>';
                 
    $config['full_tag_open'] = '<ul class="page-nav">';//分页开始标签
    $config['full_tag_close'] = $val.'</ul>';//结束标签       
							       
	$CI->pagination->initialize($config);             
    return $CI->pagination->create_links();  
           
}

?>
